var constract = require('../../utils/constract.js');
var app = getApp();
var comm = require('../../utils/common.js');
Page({
  data: {
    allow:false,
    count: 0,
    noAuth: false
  },
  onLoad: function(){
    var that = this;
    if(wx.getStorageSync('noAuth')){
      that.setData({
        noAuth: wx.getStorageSync('noAuth')
      })
      return;
    }
    app.Verification(function (token) {
      wx.request({
        url: constract.index, //查看首页的名片信息
        header: { token: token },
        success: function (res) {
          console.log(res);

          if(res.data.code === '23000'){
            that.setData({
              noAuth: true
            })
            return;
          } else {
            that.setData({
              noAuth: false
            })
            wx.removeStorageSync('noAuth');
          }

          var userInfo = res.data.data;
          that.setData({
            userInfo: userInfo
          })


          wx.request({
            url: constract.defaultName, //查看首页的名片信息
            header: { token: token },
            success: function (res) {
              console.log(res);
              
              userInfo.name=res.data.name;
              userInfo.avatar = res.data.avatar;

              app.globalData.nickName = res.data.name;
              app.globalData.avatarUrl = res.data.avatar;
              
              that.setData({
                userInfo: userInfo
              })
            }
          })

          that.setData({
            // promptShow: app.globalData.globalAuth, //判断是否授权
            promptShow: true, //判断是否授权
          })
        }
      })
    })
      

  },
  onShow: function () {
    var that=this;
    if(wx.getStorageSync('noAuth')){
      console.log('getStorage');
      that.setData({
        noAuth: wx.getStorageSync('noAuth')
      })
      return;
    }
    app.Verification(function (token) {
      wx.request({
        url: constract.index, //查看首页的名片信息
        header: { token: token },
        success: function (res) {
          console.log(res);

          if(res.data.code === '23000'){
            that.setData({
              noAuth: true
            })
            return;
          } else {
            that.setData({
              noAuth: false
            })
            wx.removeStorageSync('noAuth');
          }
          // var userInfo = res.data.data.user[0];
          var userInfo = res.data.data;
          that.setData({
            userInfo: userInfo
          })

          if (res.data.data.state == 0) {

            wx.request({
              url: constract.defaultName, //查看首页的名片信息
              header: { token: token },
              success: function (res) {
                console.log(res);
                console.log('↑↑↑');
                var mydata = {};

                app.globalData.nickName = res.data.name;
                app.globalData.avatarUrl = res.data.avatar;

                mydata.name = app.globalData.nickName;
                mydata.avatar = app.globalData.avatarUrl;
                
                that.setData({
                  mydata: mydata
                })
              }
            })

          } else {
            wx.request({
              url: constract.myData,
              header: { token: token },
              success: function (res) {
                console.log(res);
                that.setData({
                  mydata: res.data.data.myCard[0],
                  count: res.data.data.count
                })
                console.log(that.data)
              }
            })
          }
        }
      });

    })  
  },
  promptCancel: function(){
    this.setData({
      promptShow: false
    })
  },
  hideAuth: function(){
    this.setData({
      getAuth: false
    })
  },
  onGotUserInfo: function(e){//允许
    if(e.detail.errMsg === "getUserInfo:fail auth deny") {
      this.setData({
        getAuth: true
      })
    } else {
      wx.removeStorageSync('noAuth');
      wx.navigateTo({
        url: `/pages/add-card/add-card?avatar=${e.detail.userInfo.avatarUrl}&name=${e.detail.userInfo.nickName}`
      })
    }
  }
})